<?php

namespace App\Http\Controllers;

use App\Helpers\ResponseHelper;
use App\Models\Config;
use App\Models\WithdrawFund;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class WithdrawFundController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $limit = $request->get('limit', 20);
        $status = $request->get('status', 1);
        $user = $request->user();
        $withdrawFundList = WithdrawFund::where('user_id', $user->id)->where('status', $status)->orderBy('id', 'desc')->paginate($limit);

        return ResponseHelper::success(data: [
            'list' => $withdrawFundList->items(),
            'total' => $withdrawFundList->total(),
        ]);


    }

    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function withdraw(Request $request): JsonResponse
    {
        $user = $request->user();
        $withdrawalFee = Config::where('id', 3)->first()->value;
        return ResponseHelper::success(data: [
            'balance' => $user->balance,
            'withdrawFee' => $withdrawalFee,
        ]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return JsonResponse|Response
     */
    public function store(Request $request): Response|JsonResponse
    {
        $user = $request->user();
        $withdrawalFee = Config::where('id', 3)->first()->value ?? 0;
        $amount = $request->input('amount');
        if (!is_int($amount)) {
            return ResponseHelper::error(message: '请填写整数');
        }
        if ($amount <= 0) {
            return ResponseHelper::error(message: '请填写提现金额');
        }
        if (bcsub($user->balance, $amount) < 0) {
            return ResponseHelper::error(message: '余额不足');
        }
        $withdrawFund = WithdrawFund::where('user_id', $user->id)->where('status', 1)->first();
        if ($withdrawFund) {
            return ResponseHelper::error(message: '您的提现还在审核中，请勿重复提现');
        }

        try {
            // 开始事务
            DB::beginTransaction();
            $actualAmount = $amount;
            if ($withdrawalFee) {
                $actualAmount = round($amount * (1 - $withdrawalFee / 100), 2);
            }
            //创建提现记录
            WithdrawFund::create([
                'user_id' => $user->id,
                'amount' => $amount,
                'actual_amount' => $actualAmount,
            ]);
            $user->balance -= $amount;
            $user->save();
            // 提交事务
            DB::commit();
        } catch (\Exception $e) {
            // 发生错误，回滚事务
            DB::rollBack();

            // 处理异常
            return ResponseHelper::error(message: $e->getMessage());
        }
        return ResponseHelper::success(message: '申请成功,等待审核');
    }
}
